package com.jvxb.manage.livable.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jvxb.common.utils.CommonUtil;
import com.jvxb.common.web.RespMsg;
import com.jvxb.manage.livable.entity.AssetsInstance;
import com.jvxb.manage.livable.service.AssetsInstanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author jvxb
 * @since 2022-01-26
 */

@RestController
@RequestMapping("/instance/v1")
public class AssetsInstanceController {

    @Autowired
    private AssetsInstanceService assetsInstanceService;

    @PostMapping("/add")
    public RespMsg add(@RequestBody AssetsInstance assetsInstance) {
        assetsInstanceService.save(assetsInstance);
        return RespMsg.ok();
    }

    @PostMapping("/delete")
    public RespMsg delete(@RequestBody AssetsInstance assetsInstance) {
        assetsInstanceService.removeById(assetsInstance);
        return RespMsg.ok();
    }

    @PostMapping("/update")
    public RespMsg update(@RequestBody AssetsInstance assetsInstance) {
        assetsInstanceService.saveOrUpdate(assetsInstance);
        return RespMsg.ok();
    }

    @GetMapping("/getById")
    public RespMsg getById(Long id) {
        AssetsInstance assetsInstance = assetsInstanceService.getById(id);
        return RespMsg.ok(assetsInstance);
    }

    @GetMapping("/list")
    public RespMsg list(Long clusterId,
                        String hostPort,
                        String role,
                        String dbType
    ) {
        QueryWrapper wrapper = new QueryWrapper();
        if (CommonUtil.notNullOrEmpty(hostPort)) {
            if (hostPort.contains(":")) {
                wrapper.like("concat(host, ':', port)", hostPort);
            } else {
                wrapper.like(AssetsInstance.HOST, hostPort);
            }
        }
        wrapper.eq(CommonUtil.notNullOrEmpty(clusterId), AssetsInstance.CLUSTER_ID, clusterId);
        wrapper.eq(CommonUtil.notNullOrEmpty(role), AssetsInstance.ROLE, role);
        wrapper.eq(CommonUtil.notNullOrEmpty(dbType), AssetsInstance.DB_TYPE, dbType);
        List<AssetsInstance> list = assetsInstanceService.list(wrapper);
        return RespMsg.ok(list);
    }

    @GetMapping("listPage")
    public RespMsg listPage(Long clusterId,
                            String hostPort,
                            String role,
                            String dbType,
                            @RequestParam(defaultValue = "10") Integer size,
                            @RequestParam(defaultValue = "1") Integer current) {
        IPage<AssetsInstance> assetsInstanceIPage = new Page<>(current, size);
        QueryWrapper wrapper = new QueryWrapper();
        if (CommonUtil.notNullOrEmpty(hostPort)) {
            if (hostPort.contains(":")) {
                wrapper.like("concat(host, ':', port)", hostPort);
            } else {
                wrapper.like(AssetsInstance.HOST, hostPort);
            }
        }
        wrapper.eq(CommonUtil.notNullOrEmpty(clusterId), AssetsInstance.CLUSTER_ID, clusterId);
        wrapper.eq(CommonUtil.notNullOrEmpty(role), AssetsInstance.ROLE, role);
        wrapper.eq(CommonUtil.notNullOrEmpty(dbType), AssetsInstance.DB_TYPE, dbType);
        assetsInstanceService.page(assetsInstanceIPage, wrapper);
        return RespMsg.ok(assetsInstanceIPage);
    }
}
